// 引入express框架
var express = require('express');
var app = express();

// 连数据库
// var mysql      = require('mysql');
// var connection = mysql.createConnection({
//   host     : 'localhost',
//   user     : 'root',
//   password : 'root',
//   database : '555_www'
// });
 
// connection.connect();
 
// connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
//   if (error) throw error;
//   console.log('The solution is: ', results[0].solution);
// });


// 获取其他商品開始
app.get('/getOtherGoods',function(req,res){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1');
  res.header("Content-Type", "application/json;charset=utf-8");
    json = [
        {
          "goodsId": 1,
          "goodsName": "香辣鸡腿堡",
          "price": 18
        },
        {
          "goodsId": 2,
          "goodsName": "田园鸡腿堡",
          "price": 15
        },
        {
          "goodsId": 3,
          "goodsName": "和风汉堡",
          "price": 15
        },
        {
          "goodsId": 4,
          "goodsName": "大包薯条",
          "price": 18
        },
        {
          "goodsId": 5,
          "goodsName": "脆皮炸鸡腿",
          "price": 20
        },
        {
          "goodsId": 6,
          "goodsName": "魔法鸡块",
          "price": 20
        },
        {
          "goodsId": 7,
          "goodsName": "可乐大杯",
          "price": 10
        },
        {
          "goodsId": 8,
          "goodsName": "雪顶咖啡",
          "price": 18
        },
        {
          "goodsId": 9,
          "goodsName": "儿童欢乐套餐",
          "price": 25
        },
        {
          "goodsId": 10,
          "goodsName": "快乐全家桶",
          "price": 99
        }
    ]
    res.send(json);
});

app.post('/getOtherGoods',function(req,res){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1');
  res.header("Content-Type", "application/json;charset=utf-8");
    json = [
        {
          "goodsId": 1,
          "goodsName": "香辣鸡腿堡",
          "price": 18
        },
        {
          "goodsId": 2,
          "goodsName": "田园鸡腿堡",
          "price": 15
        },
        {
          "goodsId": 3,
          "goodsName": "和风汉堡",
          "price": 15
        },
        {
          "goodsId": 4,
          "goodsName": "大包薯条",
          "price": 18
        },
        {
          "goodsId": 5,
          "goodsName": "脆皮炸鸡腿",
          "price": 20
        },
        {
          "goodsId": 6,
          "goodsName": "魔法鸡块",
          "price": 20
        },
        {
          "goodsId": 7,
          "goodsName": "可乐大杯",
          "price": 10
        },
        {
          "goodsId": 8,
          "goodsName": "雪顶咖啡",
          "price": 18
        },
        {
          "goodsId": 9,
          "goodsName": "儿童欢乐套餐",
          "price": 25
        },
        {
          "goodsId": 10,
          "goodsName": "快乐全家桶",
          "price": 99
        }
    ]
    res.send(json);
});
// 获取其他商品結束


// 获取分類商品开始
app.get('/getClassificationGoods',function(req,res){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1');
  res.header("Content-Type", "application/json;charset=utf-8");
  json = [
    [
      {
        "goodsId": 1,
        "goodsImg": "./static/images/香辣鸡腿堡.jpg",
        "goodsName": "香辣鸡腿堡",
        "price": 18
      },
      {
        "goodsId": 2,
        "goodsImg": "./static/images/田园鸡腿堡.jpg",
        "goodsName": "田园鸡腿堡",
        "price": 15
      },
      {
        "goodsId": 3,
        "goodsImg": "./static/images/和风汉堡.jpg",
        "goodsName": "和风汉堡",
        "price": 15
      }
    ],
    [
      {
        "goodsId": 4,
        "goodsImg": "./static/images/薯条.jpg",
        "goodsName": "大包薯条",
        "price": 18
      },
      {
        "goodsId": 5,
        "goodsImg": "./static/images/烤全腿.jpg",
        "goodsName": "脆皮炸鸡腿",
        "price": 20
      },
      {
        "goodsId": 6,
        "goodsImg": "./static/images/魔法鸡块.jpg",
        "goodsName": "魔法鸡块",
        "price": 20
      }
    ],
    [
      {
        "goodsId": 7,
        "goodsImg": "./static/images/可乐.jpg",
        "goodsName": "可乐大杯",
        "price": 10
      },
      {
        "goodsId": 8,
        "goodsImg": "./static/images/雪顶咖啡.jpg",
        "goodsName": "雪顶咖啡",
        "price": 18
      }
    ],
    [
      {
        "goodsId": 9,
        "goodsImg": "./static/images/儿童套餐.jpg",
        "goodsName": "儿童欢乐套餐",
        "price": 25
      },
      {
        "goodsId": 10,
        "goodsImg": "./static/images/全家桶.jpg",
        "goodsName": "快乐全家桶",
        "price": 99
      }
    ]
  ];
  res.send(json);
});

app.post('/getClassificationGoods',function(req,res){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1');
  res.header("Content-Type", "application/json;charset=utf-8");
  json = [
    [
      {
        "goodsId": 1,
        "goodsImg": "./static/images/香辣鸡腿堡.jpg",
        "goodsName": "香辣鸡腿堡",
        "price": 18
      },
      {
        "goodsId": 2,
        "goodsImg": "./static/images/田园鸡腿堡.jpg",
        "goodsName": "田园鸡腿堡",
        "price": 15
      },
      {
        "goodsId": 3,
        "goodsImg": "./static/images/和风汉堡.jpg",
        "goodsName": "和风汉堡",
        "price": 15
      }
    ],
    [
      {
        "goodsId": 4,
        "goodsImg": "./static/images/薯条.jpg",
        "goodsName": "大包薯条",
        "price": 18
      },
      {
        "goodsId": 5,
        "goodsImg": "./static/images/烤全腿.jpg",
        "goodsName": "脆皮炸鸡腿",
        "price": 20
      },
      {
        "goodsId": 6,
        "goodsImg": "./static/images/魔法鸡块.jpg",
        "goodsName": "魔法鸡块",
        "price": 20
      }
    ],
    [
      {
        "goodsId": 7,
        "goodsImg": "./static/images/可乐.jpg",
        "goodsName": "可乐大杯",
        "price": 10
      },
      {
        "goodsId": 8,
        "goodsImg": "./static/images/雪顶咖啡.jpg",
        "goodsName": "雪顶咖啡",
        "price": 18
      }
    ],
    [
      {
        "goodsId": 9,
        "goodsImg": "./static/images/儿童套餐.jpg",
        "goodsName": "儿童欢乐套餐",
        "price": 25
      },
      {
        "goodsId": 10,
        "goodsImg": "./static/images/全家桶.jpg",
        "goodsName": "快乐全家桶",
        "price": 99
      }
    ]
  ];
  res.send(json);
});
// 获取分類商品结束


// 监听在9001端口
var server = app.listen(9001, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例，访问地址为 http://%s:%s", host, port)

});